<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content=
    "application/xhtml+xml; charset=iso-8859-1" />
    <title>
      5.5.&nbsp;GCC-6.3.0 - Pass 1
    </title>
    <link rel="stylesheet" type="text/css" href="../stylesheets/lfs.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="stylesheet" href="../stylesheets/lfs-print.css" type=
    "text/css" media="print" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body class="lfs" id="lfs-8.0-systemd">
    <div class="navheader">
      <h4>
        Linux From Scratch - Version 8.0-systemd
      </h4>
      <h3>
        Chapter&nbsp;5.&nbsp;Constructing a Temporary System
      </h3>
      <ul>
        <li class="prev">
          <a accesskey="p" href="binutils-pass1.html" title=
          "Binutils-2.27 - Pass 1">Prev</a>
          <p>
            Binutils-2.27 - Pass 1
          </p>
        </li>
        <li class="next">
          <a accesskey="n" href="linux-headers.html" title=
          "Linux-4.9.9 API Headers">Next</a>
          <p>
            Linux-4.9.9 API Headers
          </p>
        </li>
        <li class="up">
          <a accesskey="u" href="chapter05.html" title=
          "Chapter&nbsp;5.&nbsp;Constructing a Temporary System">Up</a>
        </li>
        <li class="home">
          <a accesskey="h" href="../index.html" title=
          "Linux From Scratch - Version 8.0-systemd">Home</a>
        </li>
      </ul>
    </div>
    <div class="wrap" lang="en" xml:lang="en">
      <h1 class="sect1">
        <a id="ch-tools-gcc-pass1" name="ch-tools-gcc-pass1"></a>5.5.
        GCC-6.3.0 - Pass 1
      </h1>
      <div class="package" lang="en" xml:lang="en">
        <p>
          The GCC package contains the GNU compiler collection, which
          includes the C and C++ compilers.
        </p>
        <div class="segmentedlist">
          <div class="seglistitem">
            <div class="seg">
              <strong class="segtitle">Approximate build time:</strong>
              <span class="segbody">8.4 SBU</span>
            </div>
            <div class="seg">
              <strong class="segtitle">Required disk space:</strong>
              <span class="segbody">2.5 GB</span>
            </div>
          </div>
        </div>
      </div>
      <div class="installation" lang="en" xml:lang="en">
        <h2 class="sect2">
          5.5.1. Installation of Cross GCC
        </h2>
        <p>
          GCC now requires the GMP, MPFR and MPC packages. As these packages
          may not be included in your host distribution, they will be built
          with GCC. Unpack each package into the GCC source directory and
          rename the resulting directories so the GCC build procedures will
          automatically use them:
        </p>
        <div class="admon note">
          <img alt="[Note]" src="../images/note.png" />
          <h3>
            Note
          </h3>
          <p>
            There are frequent misunderstandings about this chapter. The
            procedures are the same as every other chapter as explained
            earlier (<a class="xref" href=
            "generalinstructions.html#buildinstr">Package build
            instructions</a>). First extract the gcc tarball from the sources
            directory and then change to the directory created. Only then
            should you proceed with the instructions below.
          </p>
        </div>
        <pre class="userinput">
<kbd class="command">tar -xf ../mpfr-3.1.5.tar.xz
mv -v mpfr-3.1.5 mpfr
tar -xf ../gmp-6.1.2.tar.xz
mv -v gmp-6.1.2 gmp
tar -xf ../mpc-1.0.3.tar.gz
mv -v mpc-1.0.3 mpc</kbd>
</pre>
        <p>
          The following command will change the location of GCC's default
          dynamic linker to use the one installed in <code class=
          "filename">/tools</code>. It also removes <code class=
          "filename">/usr/include</code> from GCC's include search path.
          Issue:
        </p>
        <pre class="userinput">
<kbd class="command">for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&amp;@g' \
      -e 's@/usr@/tools@g' $file.orig &gt; $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' &gt;&gt; $file
  touch $file.orig
done</kbd>
</pre>
        <p>
          In case the above seems hard to follow, let's break it down a bit.
          First we find all the files under the <code class=
          "filename">gcc/config</code> directory that are named either
          <code class="filename">linux.h</code>, <code class=
          "filename">linux64.h</code> or <code class=
          "filename">sysv4.h</code>. For each file found, we copy it to a
          file of the same name but with an added suffix of <span class=
          "quote">&ldquo;<span class="quote">.orig</span>&rdquo;</span>. Then
          the first sed expression prepends <span class=
          "quote">&ldquo;<span class="quote">/tools</span>&rdquo;</span> to
          every instance of <span class="quote">&ldquo;<span class=
          "quote">/lib/ld</span>&rdquo;</span>, <span class=
          "quote">&ldquo;<span class="quote">/lib64/ld</span>&rdquo;</span>
          or <span class="quote">&ldquo;<span class=
          "quote">/lib32/ld</span>&rdquo;</span>, while the second one
          replaces hard-coded instances of <span class=
          "quote">&ldquo;<span class="quote">/usr</span>&rdquo;</span>. Next,
          we add our define statements which alter the default startfile
          prefix to the end of the file. Note that the trailing <span class=
          "quote">&ldquo;<span class="quote">/</span>&rdquo;</span> in
          <span class="quote">&ldquo;<span class=
          "quote">/tools/lib/</span>&rdquo;</span> is required. Finally, we
          use <span class="command"><strong>touch</strong></span> to update
          the timestamp on the copied files. When used in conjunction with
          <span class="command"><strong>cp -u</strong></span>, this prevents
          unexpected changes to the original files in case the commands are
          inadvertently run twice.
        </p>
        <p>
          Finally, on x86_64 hosts, set the default directory name for 64-bit
          libraries to <span class="quote">&ldquo;<span class=
          "quote">lib</span>&rdquo;</span>:
        </p>
        <pre class="userinput">
<kbd class="command">case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
 ;;
esac</kbd>
</pre>
        <p>
          The GCC documentation recommends building GCC in a dedicated build
          directory:
        </p>
        <pre class="userinput">
<kbd class="command">mkdir -v build
cd       build</kbd>
</pre>
        <p>
          Prepare GCC for compilation:
        </p>
        <pre class="userinput">
<kbd class="command">../configure                                       \
    --target=$LFS_TGT                              \
    --prefix=/tools                                \
    --with-glibc-version=2.11                      \
    --with-sysroot=$LFS                            \
    --with-newlib                                  \
    --without-headers                              \
    --with-local-prefix=/tools                     \
    --with-native-system-header-dir=/tools/include \
    --disable-nls                                  \
    --disable-shared                               \
    --disable-multilib                             \
    --disable-decimal-float                        \
    --disable-threads                              \
    --disable-libatomic                            \
    --disable-libgomp                              \
    --disable-libmpx                               \
    --disable-libquadmath                          \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-libstdcxx                            \
    --enable-languages=c,c++</kbd>
</pre>
        <div class="variablelist">
          <p class="title">
            <strong>The meaning of the configure options:</strong>
          </p>
          <dl class="variablelist">
            <dt>
              <span class="term"><em class=
              "parameter"><code>--with-newlib</code></em></span>
            </dt>
            <dd>
              <p>
                Since a working C library is not yet available, this ensures
                that the inhibit_libc constant is defined when building
                libgcc. This prevents the compiling of any code that requires
                libc support.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--without-headers</code></em></span>
            </dt>
            <dd>
              <p>
                When creating a complete cross-compiler, GCC requires
                standard headers compatible with the target system. For our
                purposes these headers will not be needed. This switch
                prevents GCC from looking for them.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--with-local-prefix=/tools</code></em></span>
            </dt>
            <dd>
              <p>
                The local prefix is the location in the system that GCC will
                search for locally installed include files. The default is
                <code class="filename">/usr/local</code>. Setting this to
                <code class="filename">/tools</code> helps keep the host
                location of <code class="filename">/usr/local</code> out of
                this GCC's search path.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--with-native-system-header-dir=/tools/include</code></em></span>
            </dt>
            <dd>
              <p>
                By default GCC searches <code class=
                "filename">/usr/include</code> for system headers. In
                conjunction with the sysroot switch, this would translate
                normally to <code class="filename">$LFS/usr/include</code>.
                However the headers that will be installed in the next two
                sections will go to <code class=
                "filename">$LFS/tools/include</code>. This switch ensures
                that gcc will find them correctly. In the second pass of GCC,
                this same switch will ensure that no headers from the host
                system are found.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--disable-shared</code></em></span>
            </dt>
            <dd>
              <p>
                This switch forces GCC to link its internal libraries
                statically. We do this to avoid possible issues with the host
                system.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--disable-decimal-float, --disable-threads,
              --disable-libatomic, --disable-libgomp, --disable-libmpx,
              --disable-libquadmath, --disable-libssp, --disable-libvtv,
              --disable-libstdcxx</code></em></span>
            </dt>
            <dd>
              <p>
                These switches disable support for the decimal floating point
                extension, threading, libatomic, libgomp, libmpx,
                libquadmath, libssp, libvtv, and the C++ standard library
                respectively. These features will fail to compile when
                building a cross-compiler and are not necessary for the task
                of cross-compiling the temporary libc.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--disable-multilib</code></em></span>
            </dt>
            <dd>
              <p>
                On x86_64, LFS does not yet support a multilib configuration.
                This switch is harmless for x86.
              </p>
            </dd>
            <dt>
              <span class="term"><em class=
              "parameter"><code>--enable-languages=c,c++</code></em></span>
            </dt>
            <dd>
              <p>
                This option ensures that only the C and C++ compilers are
                built. These are the only languages needed now.
              </p>
            </dd>
          </dl>
        </div>
        <p>
          Compile GCC by running:
        </p>
        <pre class="userinput">
<kbd class="command">make</kbd>
</pre>
        <p>
          Compilation is now complete. At this point, the test suite would
          normally be run, but, as mentioned before, the test suite framework
          is not in place yet. The benefits of running the tests at this
          point are minimal since the programs from this first pass will soon
          be replaced.
        </p>
        <p>
          Install the package:
        </p>
        <pre class="userinput">
<kbd class="command">make install</kbd>
</pre>
      </div>
      <div class="content" lang="en" xml:lang="en">
        <p>
          Details on this package are located in <a class="xref" href=
          "../chapter06/gcc.html#contents-gcc" title=
          "6.17.2.&nbsp;Contents of GCC">Section&nbsp;6.17.2, &ldquo;Contents
          of GCC.&rdquo;</a>
        </p>
      </div>
    </div>
    <div class="navfooter">
      <ul>
        <li class="prev">
          <a accesskey="p" href="binutils-pass1.html" title=
          "Binutils-2.27 - Pass 1">Prev</a>
          <p>
            Binutils-2.27 - Pass 1
          </p>
        </li>
        <li class="next">
          <a accesskey="n" href="linux-headers.html" title=
          "Linux-4.9.9 API Headers">Next</a>
          <p>
            Linux-4.9.9 API Headers
          </p>
        </li>
        <li class="up">
          <a accesskey="u" href="chapter05.html" title=
          "Chapter&nbsp;5.&nbsp;Constructing a Temporary System">Up</a>
        </li>
        <li class="home">
          <a accesskey="h" href="../index.html" title=
          "Linux From Scratch - Version 8.0-systemd">Home</a>
        </li>
      </ul>
    </div>
  </body>
</html>
